﻿$(document).ready(function() {
    // Our Confirm method
    function Confirm(question, callback) {
        // Content will consist of the question and ok/cancel buttons
        var message = $('<p />', { text: question }),
			             cancel = $('<button />', {
			                 text: 'Yes',
			                 click: function() { callback(true); }
			             }),
			            ok = $('<button />', {
			                text: 'No',
			                click: function() { callback(false); }
			            })
			           ;

			            dialogue(message.add(ok).add(cancel), 'Confirm');
    }
    // Our Alert method
    function Alert(message) {
        // Content will consist of the message and an ok button
        var message = $('<p />', { text: message }),
			        ok = $('<button />', { text: 'Ok', 'class': 'full' });

        dialogue(message.add(ok), 'Alert!');
    }

    // Our Prompt method
    function Prompt(question, initial, callback) {
        // Content will consist of a question elem and input, with ok/cancel buttons
        var message = $('<p />', { text: question }),
			        input = $('<input />', { val: initial }),
			        ok = $('<button />', {
			            text: 'Ok',
			            click: function() { callback(input.val()); }
			        }),
			        cancel = $('<button />', {
			            text: 'Cancel',
			            click: function() { callback(null); }
			        });

        dialogue(message.add(input).add(ok).add(cancel), 'Attention!');
    }

    function dialogue(content, title) {
        /* 
        * Since the dialogue isn't really a tooltip as such, we'll use a dummy
        * out-of-DOM element as our target instead of an actual element like document.body
        */
        $('<div />').qtip(
		        {
		            content: {
		                text: content,
		                title: title
		            },
		            position: {
		                my: 'center', at: 'center', // Center it...
		                target: $(window) // ... in the window
		            },
		            show: {
		                ready: true, // Show it straight away
		                modal: {
		                    on: true, // Make it modal (darken the rest of the page)...
		                    blur: false // ... but don't close the tooltip when clicked
		                }
		            },
		            hide: false, // We'll hide it maunally so disable hide events
		            style: 'qtip-light qtip-rounded qtip-dialogue', // Add a few styles
		            events: {
		                // Hide the tooltip when any buttons in the dialogue are clicked
		                render: function(event, api) {
		                    $('button', api.elements.content).click(api.hide);
		                },
		                // Destroy the tooltip once it's hidden as we no longer need it!
		                hide: function(event, api) { api.destroy(); }
		            }
		        });
    }
    //Checked
    $('#toggle-all').click(function() {
        //
        if ($(this).attr('class') != "toggle-checked") {
            $('#mainform input[type=checkbox]').attr('checked', false);
        }
        //alert($(this).attr('class'));
        $('#mainform input').checkBox();
        $('#toggle-all').toggleClass('toggle-checked');
        $('#mainform input[type=checkbox]').checkBox('toggle');
        return false;
    });
    $('#mainform input:checkbox:not(#toggle-all)').click(function() {
        $('#toggle-all').removeClass('toggle-checked');
    });
    //prompt for delete button
    $('.remove-btn').click(function(e) {
        e.preventDefault(); //prevent default action for anchor
        var self = this;
        Confirm("Confirm removed the selected records?", function(yes) {
            if (yes) {
                $("#content-outer").showLoading();    
                window.location = self.href;
            }

        });

    });
    //bulk process action
    $('.action-invoke').click(function(e) {

        e.preventDefault(); //prevent default action for anchor

        //ensure at least one item is checked
        var fields = $("input[name='selected']").serializeArray();
        if (fields.length == 0) {
            Alert('Please select at least one item.');
            return false;
        } else {
            $("#content-outer").showLoading();
            //submit form
            $(".list_item_form").attr("action", this.href);

            $(".list_item_form").submit();
        }
    });
    //bulk process delete action
    $('.action-invoke-removed').click(function(e) {

        e.preventDefault(); //prevent default action for anchor

        //ensure at least one item is checked
        var fields = $("input[name='selected']").serializeArray();
        if (fields.length == 0) {
            Alert('Please select at least one item.');
            return false;
        } else {
            var self = this;
            Confirm("Confirm removed the selected records?", function(yes) {
            if (yes) {
                    $("#content-outer").showLoading();
                    $(".list_item_form").attr("action", self.href);
                    $(".list_item_form").submit();
                }

            });
           
        }
    });
    // 1 - START DROPDOWN SLIDER SCRIPTS ------------------------------------------------------------------------

    $(".action-slider").click(function() {
        $("#actions-box-slider").slideToggle("fast");
        $(this).toggleClass("activated");
        return false;
    });

    $('a[title]').qtip();
    //  END ----------------------------- 1
  
         
   
});
